package com.fulin.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 罗马数字转阿拉伯数字
 *
 * @Author fulin
 * @Date 2022/9/7 14:40
 **/
public class Num13 {

//    private String[] albs   = new String[]{"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
//    private int[] nums      = new int[]{1000,900 ,500,400,100,90,50,40,10,9,5,4,1};

    private static Map<String,Integer> pool = new HashMap<String,Integer>(){{
        put("M",1000);
        put("CM",900);
        put("D",500);
        put("CD",400);
        put("C",100);
        put("XC",90);
        put("L",50);
        put("XL",40);
        put("X",10);
        put("IX",9);
        put("V",5);
        put("IV",4);
        put("I",1);
    }};

    public int romanToInt(String s) {
        if(s.length() < 2){
            return pool.get(s);
        }

        int total = 0;
        int len = s.length();
        for (int left = 0;left < len ;) {
            String one = s.substring(left,left +1);
            String two = left < len -1 ? s.substring(left,left+2) : "";
            if(pool.get(two) != null){
                total += pool.get(two);
                left += 2;
                continue;
            }else{
                total += pool.get(one);
                left += 1;
                continue;
            }
        }
        return total;
    }
}
